/*******************************************************************************
 * Copyright (c) 2005, 2025 Profactor GmbH, ACIN,
 *                          Johannes Kepler University Linz,
 *                          Primetals Technologies Austria GmbH,
 *                          Martin Erich Jobst
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Thomas Strasser, Ingomar Müller, Alois Zoitl, Gerhard Ebenhofer
 *     Ingo Hegny, Monika Wenger
 *                - initial implementation and rework communication infrastructure
 *   Ernst Blecha - add multibit partial access
 *   Martin Melik Merkumians - changes multibit access index from template parameter
 *                  to call argument, make TForteDWord constructor explicit, adds
 *                  implicit cast constructor, removed built-in type operator=,
 *                  added castable CIEC types operator=
 *   Martin Jobst - add user-defined literal
 *   Alois Zoitl  - migrated data type toString to std::string
 *******************************************************************************/

#pragma once

#include "forte/datatypes/forte_any_bit.h"
#include "forte/datatypes/forte_bool.h"
#include "forte/datatypes/forte_byte.h"
#include "forte/datatypes/forte_word.h"

#include "forte/datatypes/forte_any_int.h"
#include "forte/datatypes/forte_any_bit_partial.h"

#include <limits>

namespace forte {
  /*!\ingroup COREDTS CIEC_DWORD represents the dword data type according to IEC 61131.
   */
  class CIEC_DWORD : public CIEC_ANY_BIT {
      DECLARE_FIRMWARE_DATATYPE(DWORD)

    public:
      using TValueType = TForteDWord;
      [[deprecated("Please use the corresponding numeric_limits template")]]
      constexpr static size_t scmBitLength = 32U;
      [[deprecated("Please use the corresponding numeric_limits template")]]
      static constexpr TValueType scmMinVal = std::numeric_limits<TValueType>::min();
      [[deprecated("Please use the corresponding numeric_limits template")]]
      static constexpr TValueType scmMaxVal = std::numeric_limits<TValueType>::max();

      CIEC_DWORD() = default;

      CIEC_DWORD(const CIEC_DWORD &paValue) : CIEC_ANY_BIT() {
        setValueSimple(paValue);
      }

      CIEC_DWORD(const CIEC_WORD &paValue) : CIEC_ANY_BIT() {
        setValueSimple(paValue);
      }

      CIEC_DWORD(const CIEC_BYTE &paValue) : CIEC_ANY_BIT() {
        setValueSimple(paValue);
      }

      CIEC_DWORD(const CIEC_BOOL &paValue) : CIEC_ANY_BIT() {
        setValueSimple(paValue);
      }

      explicit CIEC_DWORD(const TValueType paValue) {
        setTUINT32(paValue);
      }

      ~CIEC_DWORD() override = default;

      CIEC_DWORD &operator=(const CIEC_DWORD &paValue) {
        // Simple value assignment - no self assignment check needed
        setValueSimple(paValue);
        return *this;
      }

      CIEC_DWORD &operator=(const CIEC_WORD &paValue) {
        // Simple value assignment - no self assignment check needed
        setValueSimple(paValue);
        return *this;
      }

      CIEC_DWORD &operator=(const CIEC_BYTE &paValue) {
        // Simple value assignment - no self assignment check needed
        setValueSimple(paValue);
        return *this;
      }

      CIEC_DWORD &operator=(const CIEC_BOOL &paValue) {
        // Simple value assignment - no self assignment check needed
        setValueSimple(paValue);
        return *this;
      }

      /*! \brief Converts CIEC_DWORD to elementary word
       *
       *   Conversion operator for converting CIEC_DWORD to elementary word
       */
      operator TForteDWord() const {
        return getTUINT32();
      }

      EDataTypeID getDataTypeID() const override {
        return e_DWORD;
      }

      /*! \brief Partial access within a CIEC_DWORD (e.g. [DWORD].partial<CIEC_BOOL>(1))
       *
       */
      template<class T>
      CIEC_ANY_BIT_PARTIAL<T, CIEC_DWORD> partial(size_t paIndex) {
        return CIEC_ANY_BIT_PARTIAL<T, CIEC_DWORD>(*this, paIndex);
      }

      template<class T>
      T partial(size_t paIndex) const {
        return CIEC_ANY_BIT_PARTIAL<T, CIEC_DWORD>::partialValue(*this, paIndex);
      }

      template<class T>
      T cpartial(size_t paIndex) const {
        return partial<T>(paIndex);
      }

      template<class T>
      CIEC_ANY_BIT_PARTIAL<T, CIEC_DWORD> partial(const CIEC_ANY_INT &paIndex) {
        return partial<T>(paIndex.getUnsignedValue());
      }

      template<class T>
      T partial(const CIEC_ANY_INT &paIndex) const {
        return partial<T>(paIndex.getUnsignedValue());
      }

      template<class T>
      T cpartial(const CIEC_ANY_INT &paIndex) const {
        return partial<T>(paIndex);
      }
  };

  inline CIEC_DWORD operator""_DWORD(unsigned long long int paValue) {
    return CIEC_DWORD(static_cast<CIEC_DWORD::TValueType>(paValue));
  }

  template<>
  struct CDataTypeTrait<CIEC_DWORD> {
      static constexpr CIEC_ANY::EDataTypeID scmDataTypeId = CIEC_ANY::e_DWORD;
      static const StringId scmDataTypeName;
  };
} // namespace forte

namespace std {
  template<>
  struct numeric_limits<forte::CIEC_DWORD> : public forte::templates::numeric_limits<forte::CIEC_DWORD> {
      static constexpr size_t bitLength = 32U;
  };
} // namespace std
